from rest_framework.generics import ListAPIView,CreateAPIView,ListCreateAPIView
from meiduo_admin.serializers.users import UserSerializer
from meiduo_admin.utils import PageNum
from users.models import User
from rest_framework.permissions import IsAdminUser


class UserView(ListCreateAPIView):
    # 指定序列化器属性
    serializer_class = UserSerializer
    # 指定查询集
    # queryset = User.objects.all()

    # 指定分页器
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

# 重写获取数据的方法
    def get_queryset(self):
        param_data = self.request.query_params.get('keyword')
        if param_data == '' or param_data is None:
            user = User.objects.all()
        else:
            user = User.objects.filter(username=param_data)
        return user
